package com.ibm.db2oncampus.portal.seguranca.persistence.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;

import com.ibm.db2oncampus.portal.framework.orm.hibernate.support.HibernateDaoSupport;
import com.ibm.db2oncampus.portal.seguranca.domain.Module;
import com.ibm.db2oncampus.portal.seguranca.persistence.ModuleDao;
import com.ibm.db2oncampus.portal.usuario.domain.Role;

/**
 * 
 * @author Wellington
 *
 */
public class ModuleDaoImpl extends HibernateDaoSupport<Module> implements ModuleDao {

    public ModuleDaoImpl() {
    }

    public List<Module> findModulosMenu()
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(Module.class);
        criteria.add(Property.forName("itemMenu").eq(Boolean.valueOf(true)));
        criteria.add(Property.forName("modulo").isNull());
        criteria.addOrder(Order.asc("ordemMenu"));
        List<Module> modulos = getHibernateTemplate().findByCriteria(criteria);
        return modulos;
    }

    public List<Module> find() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Module.class);
        criteria.addOrder(Order.asc("ordemMenu"));
        List<Module> modulos = getHibernateTemplate().findByCriteria(criteria);
        return modulos;
    }
    
    public List<Module> findModulosMenuByRole(List<Role> roles) {
    	Set<Module> modules = new HashSet<Module>();
    	
    	for (Role role : roles) {
    		
    		if (role.getModules() != null && (! role.getModules().isEmpty())) {
    			modules.addAll(role.getModules());
    		}
    	}
    	
    	return new ArrayList<Module>(modules);
    }
}